
import GuideEventName from "./GuideEventName";
import GuideItemModel from "./GuideItemModel";
import GuideInfo from "./GuideInfo";
import BaseModel from "../../cfw/mvc/BaseModel";
import XlsxData from "../../cfw/xlsx/XlsxData";
import TSMap from "../../cfw/struct/TSMap";
import ModuleManager from "../../cfw/module/ModuleManager";
import LocalMap from "../../cfw/storage/LocalMap";
import { ItemState } from "../../cfw/tools/Define";
export default class GuideManager extends BaseModel {

    private static ins: GuideManager;

    static instance(): GuideManager {
        if (!this.ins) {
            this.ins = new GuideManager();
        }
        return this.ins;
    }

    private saveState: LocalMap;

    init(accout: string) {
        this.saveState = new LocalMap(accout + 'saveState', ItemState.NOT_GET)
    }


    // private guideItemModel: GuideItemModel;

    private step: number = -1;

    // private list: number[] = []

    // private checkMap = {}

    // private layerMap = {}

    private guideID: number = 0;


    protected guideMap: TSMap<number, GuideInfo> = new TSMap();

    constructor() {
        super();

        let guideOpen: XlsxData = ModuleManager.dataManager.get('guide_open')
        let config: XlsxData = ModuleManager.dataManager.get('guide_info')
        guideOpen.forEach((id, value: any[]) => {
            let guideInfo = new GuideInfo(id, value)
            this.guideMap.set(id, guideInfo)
            let indexList = config.getIndexByID(1, id)
            for (let index = 0; index < indexList.length; index++) {
                const element = indexList[index];
                guideInfo.addStep(new GuideItemModel(element, config.getRowData(element)))
            }
        })


    }

    isHaveGuide() {
        return this.step >= 0;
    }

    getGuideState(id) {
        return this.saveState.get(id)
    }

    setGuideState(id, s) {
        this.saveState.set(id, s)
    }


    click() {

    }

    start(guideID: any) {
        if (this.isHaveGuide()) {
            return;
        }
        if (!this.guideMap.has(guideID)) {
            return;
        }
        if (this.isFinish(guideID)) {
            return;
        }
        let guideInfo = this.guideMap.get(guideID)
        let flag = false;
        let value = guideInfo.getConditionValue();
        switch (guideInfo.getConditionType()) {
            case 0:
                if (value == 0) {
                    flag = true;
                } else {
                    flag = this.getGuideState(value) == 1;
                }

                break;
        }
        if (flag) {
            // cc.log(' GuideManager start guideID ', guideID)
            this.step = 0;
            this.guideID = guideID;
            this.emit(GuideEventName.START)
        }
    }

    isFinish(guideID) {
        return this.getGuideState(guideID) == ItemState.GOT;
    }



    addItem(layer: cc.Node, id?: any) {
        if (!this.isHaveGuide()) {
            return;
        }
        let item = this.getItemModel(this.guideID, this.step);
        if (item) {
            if (!item.isFull()) {
                item.checkNode(layer, id)
                if (item.isFull()) {
                    // cc.log(' addItem ', this.guideID, this.step, ' id ', id)
                    this.emit(GuideEventName.UPDATE)
                }
            }



        }
    }

    getItemModel(guideID: any = this.guideID, step: number = this.step) {
        if (!this.guideMap.has(guideID)) {
            return null;
        }
        let list = this.guideMap.get(guideID).getStepList()
        if (list.length > 0 && step >= 0) {
            return list[step]
        }
        return null;
    }

    end() {
        this.step = -1;
        this.setGuideState(this.guideID, 1)
        this.emit(GuideEventName.END)
        this.emit(GuideEventName.FINISH)
    }

    update() {
        this.emit(GuideEventName.UPDATE_STEP)
    }

    check(layerName: string, funcName: string, id?: any, func?: Function) {
        if (!this.isHaveGuide()) {
            if (func) {
                func();
            }
            return true;
        }
        let item = this.getItemModel();
        if (item) {
            if (item.check(layerName, funcName, id)) {
                if (func) {
                    func();
                }
                this.next();
                return true;
            } else {
                if (item.isForce()) {
                    return false;
                } else {
                    if (func) {
                        func();
                    }
                    this.end();
                    return true;
                }

            }
        }

    }

    next() {
        this.step++;
        let list = this.guideMap.get(this.guideID).getStepList();
        if (list) {
            if (this.step >= list.length) {
                this.end();
            } else {
                this.update();
            }
        }

    }
}
